********************************************************************************
*** Technology Diffusion and the International System Replication Files
***
*** This runs/outputs all tables in the maintext and appendix 
***
*** Required data files: "tech_data.dta", "full_data.dta", and "imputed_clean.dta"
***
*** Note: Ensure that there is a subfolder in the directory from which you are 
*** running these analyses named "output" to allow for the tables to be generated
***
*** Created by: Thomas Cunningham
*** Date: 27 January 2021
******************************************************************

********************************************************************************
*** Table 1: Summary Statistics
********************************************************************************
clear all
version 16.1 
cd 
use "full_data.dta", clear
estpost tabstat log_adopt delta, statistics(n mean sd min max) columns(statistics)
esttab using "output/table1.tex", cells(n mean sd min max) replace

use "full_data.dta", clear
drop if log_adopt == . 
estpost tabstat sdt, statistics(n mean sd min max) columns(statistics)
esttab using "output/table1.tex", cells(n mean sd min max) append

use "full_data.dta", clear
duplicates drop year ccode sysconall, force 
estpost tabstat sysconall, statistics(n mean sd min max) columns(statistics)
est store syscon
esttab syscon using "output/table1.tex", cells(n mean sd min max) append

use "full_data.dta", clear
duplicates drop year ccode dlog_milex, force
estpost tabstat dlog_milex, statistics(n mean sd min max) columns(statistics)
est store milex
esttab milex using "output/table1.tex", cells(n mean sd min max) append

use "full_data.dta", clear
duplicates drop year ccode polity2, force
estpost tabstat polity2, statistics(n mean sd min max) columns(statistics)
est store polity 
esttab polity using "output/table1.tex", cells(n mean sd min max) append

use "full_data.dta", clear
duplicates drop year ccode warL1, force
estpost tabstat warL1, statistics(n mean sd min max) columns(statistics)
est store war
esttab war using "output/table1.tex", cells(n mean sd min max) append

use "full_data.dta", clear
duplicates drop year ccode cwarL1, force
estpost tabstat cwarL1, statistics(n mean sd min max) columns(statistics)
est store cwar
esttab cwar using "output/table1.tex", cells(n mean sd min max) append


********************************************************************************
*** Table 2: Country-Year-Technology Tests: Technology Adoption and Systemic Factors
********************************************************************************
clear all
use "tech_data.dta", clear

xtset cou_tech_enc year

local covars "sdt polity2 warL1 cwarL1"
local model1 "sysconall"
local model2 "sysconall dlog_milex"
local model3 "sysconall dlog_milex log_gdppc"


	forval j=1/3{
		reg delta `model`j'' `covars', vce(cluster cou_tech_enc)
		est store model`j'
	}
	
esttab model1 model2 model3 using "output/table2.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(sysconall dlog_milex log_gdppc sdt polity2 warL1 cwarL1) title("Country-Year-Technology Tests) \\ (Technology Adoption and Systemic Factors 1820-2008)} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Country-Technology clustered standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 

estimates clear
********************************************************************************
*** Table 3: Systemic Tests: World-Wide Technology Adoption and System Concentration
********************************************************************************
clear all
use "tech_data.dta", clear

xtset cou_tech_enc year
keep ww_delta ww_sysconall ww_polity2_other year
xtset, clear 
duplicates drop 
tsset year
local sysmodel1 "ww_sysconall"
local sysmodel2 "ww_sysconall ww_polity2_other"


	forval j=1/2{
		reg ww_delta `sysmodel`j'', vce(robust)
		est store sysmodel`j'
	}
	
esttab sysmodel1 sysmodel2 using "output/table3.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse F, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(ww_sysconall ww_polity2_other) title("Systemic Tests) \\ (World-Wide Technology Adoption and System Concentration 1820-2008)} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Robust standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 
estimates clear 

********************************************************************************
*** Table 4: Granger Causation: World-Wide Technology Adoption and System Concentration
*** AND 
*** Table 12: Granger Causality Analysis
********************************************************************************
clear all 
use "tech_data.dta", clear
xtset, clear
keep year granger_*
renvars granger_*, predrop(8)
duplicates drop
keep if year < 2009
drop if year < 1824
tsset year
local gmodel "dww_delta dww_resid"
putexcel set output/granger1, replace
local k = 1
foreach mod in `gmodel'{
	forval a = 1/4{
var `mod' dsysconall, lags(1/`a')
vargranger
matrix list r(gstats)
di
putexcel A`k' = matrix(r(gstats))
local k = `k'+10
estimates clear
	}
}
	
import excel "output/granger1.xlsx", clear
drop A
rename B lags
rename C pval
duplicates drop 
drop if lags == .
gen obs = _n
gen model = ""
replace model = "SystemcausesTech_Adoption" if inlist(obs, 1, 3, 5, 7)
replace model = "Tech_AdoptioncausesSystem" if inlist(obs, 2, 4, 6, 8)
replace model = "SystemcausesTech_Adoption#" if inlist(obs, 9, 11, 13, 15)
replace model = "Tech_AdoptioncausesSystem#" if inlist(obs, 10, 12, 14, 16)
drop obs
gen sign = "Yes"
replace sign = "No" if pval > 0.05
replace sign  = "Yes{***}" if pval < 0.01
replace sign  = "Yes{**}" if pval < 0.05 & pval > 0.01
drop pval 
tostring lags, replace
gen lag = "1 - " + lags
drop lags
order lag model sign
outfile using "output/granger.tex", replace

import excel "output/granger1.xlsx", clear
rename A chi2
rename B df 
rename C pval 
gen nobs = _n
duplicates drop chi2 df pval, force
drop if pval == .
gen model = ""
replace model = "SystemcausesTech_Adoption" if inlist(nobs, 1, 11, 21, 31)
replace model = "Tech_AdoptioncausesSystem" if inlist(nobs, 3, 13, 23, 33)
replace model = "SystemcausesTech_Adoption#" if inlist(nobs, 41, 51, 61, 71)
replace model = "Tech_AdoptioncausesSystem#" if inlist(nobs, 43, 53, 63, 73)
gen sign = "Yes"
replace sign = "No" if pval > 0.05
replace sign  = "Yes{***}" if pval < 0.01
replace sign  = "Yes{**}" if pval < 0.05 & pval > 0.01
outfile using "output/appendixtab12.tex", replace



********************************************************************************
********************************************************************************
*** APPENDIX ANALYSES
********************************************************************************
********************************************************************************


********************************************************************************
*** Appendix Table 1: Viable Great Power Coalitions/C4
********************************************************************************
clear all
use "tech_data.dta", clear

xtset cou_tech_enc year

local covars "sdt polity2 warL1 cwarL1"
local model1 "nviable"
local model2 "nviable dlog_milex log_gdppc"
local model3 "c4"
local model4 "c4 dlog_milex log_gdppc"


	forval j=1/4{
		reg delta `model`j'' `covars', vce(cluster cou_tech_enc)
		est store model`j'
	}
	
esttab model1 model2 model3 model4 using "output/appendixtab1.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(nviable c4 sdt log_gdppc polity2 warL1 cwarL1 dlog_milex) title("Technology Adoption and Systemic Factors 1820-2008} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Country-Technology clustered standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 

estimates clear



********************************************************************************
*** Appendix Table 2: Systemic Analysis with Alternative IVs
********************************************************************************
clear all
use "tech_data.dta", clear

xtset cou_tech_enc year

keep ww_delta ww_sysconall ww_polity2_other year nviable c4 
xtset, clear 
duplicates drop 
tsset year
local expvar "ww_sysconall nviable c4"
local covars1 ""
local covars2 "ww_polity2_other"

local k = 1
	forval j=1/2{
		foreach exp in `expvar'{
		reg ww_delta `exp' `covars`j'', vce(robust)
		est store model`k'
		local k = `k'+1
	}
	}
	
esttab model1 model2 model3 model4 model5 model6 using "output/appendixtab2.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse F, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(ww_sysconall nviable c4 ww_polity2_other) title("Systemic Analysis with Alternative IVs} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Robust standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 

estimates clear


********************************************************************************
*** Appendix Table 3: Systemic Variable Calculated without Country i's Contribution
*** and Systemic Variables Calculated Without Iron and Steel Production
********************************************************************************
clear all
use "tech_data.dta", clear

xtset cou_tech_enc year

local expvar1 "syscon_nocou nviablenocou c4_nocou"
local expvar2 "syscon_notech nviablenotech c4_notech"

local k = 1
forval j = 1/2{
foreach exp in `expvar`j''{
reg delta `exp' sdt polity2 warL1 cwarL1, vce(cluster cou_tech_enc)
est store model`k'
local k = `k' + 1
}
}
	
esttab model1 model2 model3 model4 model5 model6 using "output/appendixtab3.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(syscon_nocou nviablenocou c4_nocou syscon_notech nviablenotech c4_notech sdt polity2 warL1 cwarL1 ) title("Systemic Variables Calculated Without Country is Contribution and Systemic Variables Calculated Without Iron and Steel Production} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Country-Technology clustered standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 

estimates clear



********************************************************************************
*** Appendix Table 4: World Military Spending / Wars / MIDs as Dependent Variable
********************************************************************************

use "tech_data.dta", clear 
xtset cou_tech_enc year
keep log_ww_milex log_ww_war ww_sysconall nviable c4 year log_ww_mid entmidpct
xtset, clear 
duplicates drop 
tsset year

local expvar "ww_sysconall nviable c4"
local dvs "log_ww_milex log_ww_war log_ww_mid entmidpct"

local k = 1
	foreach depvar in `dvs'{
		foreach exp in `expvar'{
		reg `depvar' `exp', vce(robust)
		est store model`k'
		local k = `k'+1
	}
	}
	
esttab model1 model2 model3 model4 model5 model6 model7 model8 model9 model10 model11 model12 using "output/appendixtab4.tex", replace cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse F, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(ww_sysconall nviable c4) title("Worldwide Military Spending / Wars as Dependent Variable} \begin{threeparttable") nonumber  nonotes compress postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Robust standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 

estimates clear

********************************************************************************
*** Appendix Table 5: Explanatory Variable MID 
********************************************************************************

use "tech_data.dta", clear

xtset cou_tech_enc year

local covars "sdt polity2 warL1 cwarL1 dlog_milex"
local expvar "log_ww_milex log_ww_war log_ww_mid entmidpct"

local k = 1
	foreach exp of varlist `expvar'{
		reg delta `exp' `covars', vce(cluster cou_tech_enc)
		est store model`k'
		local k = `k' + 1
	}
	
esttab model1 model2 model3 model4 using "output/appendixtab5.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(log_ww_milex log_ww_war log_ww_mid entmidpct sdt polity2 warL1 cwarL1 dlog_milex) title("Technology Adoption and Systemic Factors 1820-2008} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Country-Technology clustered standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 

estimates clear



********************************************************************************
*** Appendix Table 6: Main Specification with Alternative Samples
********************************************************************************
clear all
use "tech_data.dta", clear
xtset cou_tech_enc year

local indvars "sysconall sdt polity2 warL1 cwarL1"

preserve
use "imputed_clean.dta", clear
mi est, post: reg dln_adopt `indvars', vce(cluster cou_tech_enc)
est store sample1
restore 

preserve 
keep if year > 1899 & year < 2001
reg delta `indvars', vce(cluster cou_tech_enc)
est store sample2
restore 

preserve 
keep if majpow == 0
reg delta `indvars', vce(cluster cou_tech_enc)
est store sample3
restore 

preserve 
keep if majpow == 1
reg delta `indvars', vce(cluster cou_tech_enc)
est store sample4
restore

preserve 
keep if eur == 1
reg delta `indvars', vce(cluster cou_tech_enc)
est store sample5
restore

preserve 
keep if eur == 0
reg delta `indvars', vce(cluster cou_tech_enc)
est store sample6
restore


preserve
drop if inlist(technology, "railline", "railpkm")
reg delta `indvars', vce(cluster cou_tech_enc)
est store sample7
restore 

preserve 
drop if inlist(technology, "aviationpkm", "aviationtkm", "ship_all", "vehicle_car", "vehicle_com")
reg delta `indvars', vce(cluster cou_tech_enc)
est store sample8
restore 

preserve 
drop if inlist(technology, "cellphone", "computer", "radio", "telegram", "telephone", "tv")
reg delta `indvars', vce(cluster cou_tech_enc)
est store sample9
restore 

preserve 
drop if inlist(technology, "steel_bof", "steel_eaf")
reg delta `indvars', vce(cluster cou_tech_enc)
est store sample10
restore

preserve 
drop if inlist(technology, "tv", "atm")
reg delta `indvars', vce(cluster cou_tech_enc)
est store sample11
restore


	
esttab sample1 sample2 sample3 sample4 sample5 sample6 sample7 sample8 sample9 sample10 sample11  using "output/appendixtab6.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(sysconall sdt polity2 warL1 cwarL1) title("Main Specification with Alternative Samples} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Country-Technology clustered standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 

estimates clear



********************************************************************************
*** Appendix Table 7: Main Specification with Additional Controls 
********************************************************************************
clear all
use "tech_data.dta", clear
xtset cou_tech_enc year

local covars "sysconall sdt polity2 warL1 cwarL1"
local addvars "log_pop_mad coldwar democracy post_worldwar"

local k = 1
	foreach avar of varlist `addvars'{
		reg delta `covars' `avar', vce(cluster cou_tech_enc)
		est store model`k'
		local k = `k' + 1
	}

reg delta `covars' `addvars', vce(cluster cou_tech_enc)
est store model5 

	
esttab model1 model2 model3 model4 model5 using "output/appendixtab7.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(sysconall sdt polity2 warL1 cwarL1 log_pop_mad coldwar democracy post_worldwar) title("Main Specification with Additional Controls} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Country-Technology clustered standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 

estimates clear

********************************************************************************
*** Appendix Table 8: New Systemic Controls: Inventions 
********************************************************************************
clear all
use "tech_data.dta", clear
xtset cou_tech_enc year
keep ww_delta ww_sysconall nviable c4 ww_adopt_ma ww_adopt_avg year ww_delta_avg ww_delta_ma ww_sysconall_ma ww_c4_ma ww_nviable_ma ww_adopt_ma_exp ww_adopt_avg_exp year ww_delta_avg_exp ww_delta_ma_exp ww_sysconall_ma_exp ww_c4_ma_exp ww_nviable_ma_exp log_ww_milex
xtset, clear 
duplicates drop 
tsset year

local expvar1 "ww_sysconall" 
local expvar2 "ww_sysconall_ma"
local expvar3 "ww_sysconall" 
local expvar4 "ww_sysconall_ma_exp"


local addvar1 "ww_adopt_avg ww_adopt_ma"
local depvar1 "ww_delta_avg"
local depvar2 "ww_delta_ma"

local addvar2 "ww_adopt_avg_exp ww_adopt_ma_exp"
local depvar3 "ww_delta_avg_exp"
local depvar4 "ww_delta_ma_exp"

local k = 1

local j = 1
	while `j' < 3{
		foreach add in `addvar1'{
		reg `depvar`j'' `expvar`j'' `add', vce(robust)
		est store model`k'
		local k = `k' + 1
		local j = `j' + 1
	}
	
	while `j' > 2 & `j' < 5{
		foreach ad in `addvar2'{
		reg `depvar`j'' `expvar`j'' `ad', vce(robust)
		est store model`k'
		local k = `k' + 1
		local j = `j' + 1
	}
	}
	

	}
	
local expva "ww_sysconall"
local addva "log_ww_milex"


	foreach ex in `expva'{
		foreach ad in `addva'{
		reg ww_delta `ex' `ad', vce(robust)
		est store model5
	}
	}
	
esttab model* using "output/appendixtab8.tex", replace cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(ww_sysconall nviable c4 ww_adopt_avg ww_adopt_ma ww_adopt_avg_exp ww_adopt_ma_exp) title("New Systemic Controls: Inventions} \begin{threeparttable") nonumber  nonotes compress postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Robust standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 

estimates clear

********************************************************************************
*** Table 9a: Additional Models with Lagged Dependent Variables
*** Country-Technology Year
********************************************************************************
clear all
use "tech_data.dta", clear
xtset cou_tech_enc year
local covars "sdt polity2 warL1 cwarL1 L2.delta"
local expvar "sysconall nviable c4"
local model1 " "
local model2 "dlog_milex"
local k = 1
	forval j=1/3{
	    foreach exp in `expvar'{
		reg delta `exp' `model`j'' `covars', vce(cluster cou_tech_enc)
		est store model`k'
		local k = `k' + 1
	}
	}
	
esttab model1 model2 model3 model4 model5 model6 using "output/appendixtab9a.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(sysconall nviable c4 sdt polity2 warL1 cwarL1 dlog_milex) title("Country-Year-Technology Tests) \\ (Additional Models with Lagged Dependent Variables)} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Country-Technology clustered standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 

estimates clear

********************************************************************************
*** Appendix Table 9b: Additional Models with Lagged Dependent Variable - Systemic
********************************************************************************
clear all
use "tech_data.dta", clear
xtset cou_tech_enc year
keep ww_delta ww_sysconall ww_adopt ww_polity2_other year c4 nviable
xtset, clear 
duplicates drop 
tsset year

local depvar "ww_delta"
local expvar "ww_sysconall nviable c4"
local covar1 " "
local covar2 "ww_polity2_other"
local lagvar "L2.ww_delta"

local k = 1
		forval c = 1/2{
		    foreach exp in `expvar'{
			    reg `depvar' `covar`c'' `exp' `lagvar', vce(robust)
				est store model`k'
				local k = `k'+1
	}
	}
	
esttab model1 model2 model3 model4 model5 model6 using "output/appendixtab9b.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a rmse, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(ww_sysconall nviable c4 ww_polity2_other) title("Additional Models with Lagged Dependent Variable - Systemic} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. Robust standard errors in parentheses. \end{tablenotes} \end{threeparttable} \end{table}") 

estimates clear




********************************************************************************
*** Appendix Table 10a: GMM Dynamic Panel Data One Step Difference 
*** Country-Technology-Year Tests
********************************************************************************
clear all
use "tech_data.dta", clear
keep log_adopt sysconall c4 nviable year sdt polity2 warL1 cwarL1 cou_tech_enc dlog_milex log_gdppc
xtset cou_tech_enc year



* Switch to speed-favoring mode, rather than space-favoring, to allow for cluster()

mata: mata set matafavor speed, perm



***** Estimation 
** Uses two lags of the dependent variable as a regressor
** Assumes all independent variables/covariates are not strictly exogenous
** Collapses instruments
local expvar "sysconall c4 nviable"
local k = 1
foreach exp in `expvar'{
* DV is level var, uses 2 lags of the level var as instruments
xtabond2 log_adopt L.log_adopt L2.log_adopt L.`exp' sdt polity2 warL1 cwarL1, artests(5) gmmstyle(sdt polity2 warL1 cwarL1 L.`exp' L.log_adopt, eq(d) collapse) noleveleq robust cluster(cou_tech_enc)
est store model`k'

local k = `k' + 1

*** Model 2 (Added Covariates)

* DV is level var, uses 2 lags of the level var as instruments
xtabond2 log_adopt L.log_adopt L2.log_adopt L.`exp' sdt polity2 warL1 cwarL1 dlog_milex log_gdppc, artests(5) gmmstyle(sdt polity2 warL1 cwarL1 dlog_milex log_gdppc L.`exp' L.log_adopt, eq(d) collapse) noleveleq robust cluster(cou_tech_enc)
est store model`k'

local k = `k' + 1
}

esttab model1 model2 model3 model4 model5 model6 using "output/appendixtab10a.tex", replace cells(b(star fmt(%9.3f)) se(par)) order(L.sysconall L.c4 L.nviable sdt polity2 warL1 cwarL1 dlog_milex log_gdppc) stats(N hansenp ar1 ar1p ar2 ar2p ar3 ar3p, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) 

********************************************************************************
*** Appendix Table 10b: Continuously updating generalized method of moments estimator 
*** Systemic Tests 
********************************************************************************
clear all
use "tech_data.dta", clear
xtset cou_tech_enc year
keep ww_delta ww_sysconall c4 nviable year ww_polity2_other ww_adopt
xtset, clear
duplicates drop 
tsset year

** Optimal lag length for internal instruments 
foreach exp of varlist ww_sysconall nviable c4{
	reg ww_delta `exp', robust 
	actest, lags(20) robust
	estimates clear
	reg ww_delta `exp' ww_polity2_other, robust
	actest, lags(20) robust
	estimates clear
}


** Estimation 
local k = 1
local lag1 = 9
local lag2 = 9
local lag3 = 5
local lag4 = 5
local lag5 = 9
local lag6 = 13
local expvar "ww_sysconall c4 nviable"
foreach exp in `expvar'{
ivreg2 ww_delta (`exp' = L(1/`lag`k'').ww_delta), cue center endog(`exp') robust first 
est store systemic`k'

twostepweakiv cue ww_delta (`exp' = L(1/`lag`k'').ww_delta), level(99) gammalevel(1)
est store sysLC`k'
	
local k = `k' + 1
*** System-year analysis (Model 2 - Added Covariates)

ivreg2 ww_delta ww_polity2_other (`exp' = L(1/`lag`k'').ww_delta), cue center endog(`exp') robust first 
est store systemic`k'

twostepweakiv cue ww_delta ww_polity2_other (`exp' = L(1/`lag`k'').ww_delta), level(99) gammalevel(1)
est store sysLC`k'

local k = `k' + 1
}

esttab systemic1 systemic3 systemic5 systemic2 systemic4 systemic6 using "output/appendixtab10b.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) order(ww_sysconall c4 nviable ww_polity2_other) stats(N estatp j jp widstat arfp, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) /*
	*/ label legend nobaselevels  
	
esttab sysLC1 sysLC3 sysLC5 sysLC2 sysLC4 sysLC6 using "output/appendixtab10b.tex", append stats(lc_cset)




********************************************************************************
*** Appendix Table 11 - Panel A: Error Correction Model - Results 
*** Country Technology Year 
********************************************************************************
clear all 
use "imputed_clean.dta", clear
local k = 1
local expvar "sysconall nviable c4"
foreach exp in `expvar'{
mi est, cmdok post: xtpmg D.(ln_adopt `exp' sdt polity2 warL1 cwarL1), lr(L.(ln_adopt `exp')) ec(ECT) replace cluster(cou_tech_enc) dfe
est store ecm_cty`k'
local k = `k' + 1
}
esttab ecm_cty1 ecm_cty2 ecm_cty3 using "output/appendixtab11A.tex", replace cells(b(star fmt(%9.3f)) se(par)) stats(N, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01) order(sysconall nviable c4 sdt polity2 warL1 cwarL1) label legend nobaselevels rename(sysconall "Syscon" sdt "Spatial Dist" polity2 "Polity2 Score" warL1 "War (Lagged One Year)" cwarL1 "Civil War (Lagged One Year)")  nomtitles



********************************************************************************
*** Appendix Table 11B: Error Correction Model - Results: Systemic
********************************************************************************
clear all
use "imputed_clean.dta", clear
mi xeq: egen delta_world = mean(dln_adopt), by(year)
save "data/worldECM.dta", replace
use "data/worldECM.dta", clear
mi xeq: egen adopt_world = mean(ln_adopt), by(year)
save "data/worldECMpt1.dta", replace
use "data/worldECMpt1.dta", clear
keep delta_world adopt_world sysconall nviable c4 year imp _mi_id _mi_miss _mi_m 
mi xeq: duplicates drop delta_world sysconall nviable c4 year, force
save "data/worldECMpt2.dta", replace
use "data/worldECMpt2.dta", clear
mi tsset year



mi est, cmdok post: ardl delta_world sysconall, lags(4 0) ec1
est store ecm_sys1

mi xeq: ardl delta_world sysconall, lags(4 0) ec1
estat ectest
est store ectest1

mi est, cmdok post: ardl delta_world nviable, lags(1 0) ec1
est store ecm_sys2

mi xeq: ardl delta_world nviable, lags(1 0) ec1
estat ectest
est store ectest2

mi est, cmdok post: ardl delta_world c4, lags(1 0) ec1
est store ecm_sys3

mi xeq: ardl delta_world c4, lags(1 0) ec1
estat ectest
est store ectest3

esttab ecm_sys* using "output/appendixtab11B.tex", replace cells(b(star fmt(%9.3f)) se(par)) unstack starlevels(* .1 ** .05 *** .01) label legend nobaselevels rename(sysconall "Syscon")  nomtitles

esttab ectest* using "output/appendixtab11b.tex", append stats(F_pss t_pss)

******************************************************************************** 
